package com.ruoyi.wvp.sdk.monitor.impl;

import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.wvp.config.MonitorPlatformConfig;
import com.ruoyi.wvp.domain.Surveillance;
import com.ruoyi.wvp.sdk.monitor.IMonitorSdkService;
import com.ruoyi.wvp.sdk.monitor.MonitorApi;
import com.ruoyi.wvp.sdk.monitor.R;
import com.ruoyi.wvp.sdk.monitor.entity.params.AlarmEventPageParams;
import com.ruoyi.wvp.sdk.monitor.entity.params.LiveHistoryParams;
import com.ruoyi.wvp.sdk.monitor.entity.params.LiveParams;
import com.ruoyi.wvp.sdk.monitor.entity.params.LoginParams;
import com.ruoyi.wvp.sdk.monitor.entity.MonitorEventInfo;
import com.ruoyi.wvp.sdk.monitor.entity.PageEntity;
import com.ruoyi.wvp.sdk.monitor.entity.response.LiveHistoryInfo;
import com.ruoyi.wvp.sdk.monitor.entity.response.LiveInfo;
import com.ruoyi.wvp.sdk.monitor.entity.response.MonitorStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

import static com.ruoyi.wvp.config.MonitorPlatformConfig.CACHE_NAME;
import static com.ruoyi.wvp.config.MonitorPlatformConfig.TOKEN_KEY;

@Service
@Slf4j
public class MonitorSdkServiceImpl implements IMonitorSdkService {

    private static Integer SUCCESS_CODE = 200;

    @Autowired
    private MonitorApi monitorApi;

    @Autowired
    private MonitorPlatformConfig platformConfig;

    private static ReentrantLock LOCK = new ReentrantLock();

    public Boolean isExpire() {
        LOCK.lock();
        try {
            String token = CacheUtils.get(CACHE_NAME, TOKEN_KEY, String.class);
            if (StringUtils.isBlank(token)) {
                login();
                return true;
            }
            return false;
        } finally {
            LOCK.unlock();
        }
    }

    public void login() {
        R<String> result = monitorApi.login(new LoginParams(platformConfig.getUserName(), platformConfig.getPassword()));
        if (SUCCESS_CODE.equals(result.getCode())) {
            CacheUtils.put(CACHE_NAME, TOKEN_KEY, result.getData(), 11, TimeUnit.HOURS);
        }

    }

    @Override
    public PageEntity<MonitorEventInfo> getEventList(AlarmEventPageParams alarmEventPageParams) {
        isExpire();
        alarmEventPageParams.setProjectId(platformConfig.getProjectId());
        R<PageEntity<MonitorEventInfo>> alarmEventPageUser = monitorApi.getAlarmEventPageUser(alarmEventPageParams);
        if (SUCCESS_CODE.equals(alarmEventPageUser.getCode())) {
            return alarmEventPageUser.getData();
        }
        return null;
    }

    @Override
    public LiveInfo getLiveInfo(LiveParams params) {
        isExpire();
        R<LiveInfo> r = monitorApi.getLiveInfo(params);
        if (SUCCESS_CODE.equals(r.getCode())) {
            return r.getData();
        }
        throw new RuntimeException("获取直播地址失败");
    }

    @Override
    public LiveHistoryInfo getLiveHistoryInfo(LiveHistoryParams liveHistoryParams) {
        isExpire();
        R<LiveHistoryInfo> liveHistoryInfo = monitorApi.getLiveHistoryInfo(liveHistoryParams);
        if (SUCCESS_CODE.equals(liveHistoryInfo.getCode())) {
            return liveHistoryInfo.getData();
        }
        throw new RuntimeException("获取回放地址失败");
    }

    @Override
    public LiveInfo getLiveInfo(Surveillance surveillance) {
        LiveParams params=new LiveParams();
        params.setMonitorCode(surveillance.getDeviceNumber()+"#1");
        params.setProjectId(platformConfig.getProjectId());
        params.setExpireTime(0L);
        return getLiveInfo(params);
    }

    @Override
    public LiveHistoryInfo getLiveHistoryInfo(Surveillance surveillance) {
//        LiveParams params=new LiveParams();
//        params.setMonitorCode(surveillance.getDeviceNumber()+"#1");
//        params.setProjectId(platformConfig.getProjectId());
//        params.setExpireTime(0L);
        LiveHistoryParams params=new LiveHistoryParams();
        params.setType(2);
        params.setProtocol(4);
        params.setStartTime(surveillance.getStartTime());
        params.setStopTime(surveillance.getStopTime());
//        params.setExpireTime(0);
        params.setDeviceSerial(surveillance.getDeviceNumber()+"#1");
        return getLiveHistoryInfo(params);
    }

    @Override
    public List<MonitorStatus> getListMonitorStatus() {
        isExpire();
        R<List<MonitorStatus>> r = monitorApi.getMonitorStatus();
        if (SUCCESS_CODE.equals(r.getCode())) {
            return r.getData();
        }
        return List.of();
    }
}
